//
//  OptionsViewModel.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/3/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.


import Foundation

class OptionViewModel {
    struct Option {
        var isSingle: Bool?=false
        var name: String
        var items: [Option]?=nil
        var isSelected: Bool?=false
    }
    
    var option: Option!
    
    var title: String {
        return option.name
    }
    var isExpanded: Bool?=false
    
    init (option: [String: Any]) {
        if option["items"] != nil {
            let optionItems = (option["items"] as! [[String: Any]]).map {
                x in Option(isSingle: true, name: x["name"] as! String, isSelected: (x["isSelected"] as? Bool) ?? false)
            }

            let _option = Option(isSingle: option["items"] == nil,
                                 name: option["name"] as! String,
                                 items: optionItems)
            self.option = _option
        } else {
            self.option = Option(isSingle: true,
                                 name: option["name"] as! String,
                                 isSelected: (option["isSelected"] as? Bool) ?? false)
        }
    }
}


class OptionsViewModel {
    
    var options: [String: [OptionViewModel]] = [:]
    var sectionOrder = ["Обзор", "Элементы экстерьера", "Мультимеди", "Салон", "Комфорт", "Безопасность", "Прочее"]
    
    // MARK: - Convert to OptionViewModel format
    
    func setUpOptions (options: [[String: Any]], completion: (() -> Void)?=nil) {
        for section in options {
            let title = section["name"] as! String
            let items = section["items"] as! [[String: Any]]
            var _optionViewModels:[OptionViewModel] = []
            for item in items {
                let _option = OptionViewModel(option: item)
                _optionViewModels.append(_option)
            }
            self.options[title] = _optionViewModels
        }
        completion?()
    }
    
    // MARK: - Convert OptionViewModel to [[String: Any]]
    
    func formatOptions () -> [[String: Any]] {
        var normalOptions: [[String: Any]] = []
        for title in options.keys {
            var _options: [String: Any] = [:]
            var _items:[[String:Any]] = []
            for option in options[title]! {
                var _option: [String: Any] = [:]
                _option["name"] = option.option.name
                
                if let items = option.option.items {
                    _option["items"] = items.map {
                        x in [
                            "isSelected": x.isSelected,
                            "name": x.name
                        ]
                    }
                } else {
                    _option["isSelected"] = option.option.isSelected
                }
                _items.append(_option)
            }
            _options["name"] = title
            _options["items"] = _items
            normalOptions.append(_options)
        }
        return normalOptions
    }
    
    // MARK: - Reset all parameters
    
    func reset () {
        for title in options.keys {
            options[title]!.map { x in
                x.option.isSelected = false
                if x.option.items != nil {
                    for (i, _) in x.option.items!.enumerated() {
                        x.option.items![i].isSelected = false
                    }
                }
            }
        }
    }
    
    // MARK: - Get Selected Items
    
    func getSelectedItems () -> [OptionViewModel.Option] {
        var output: [OptionViewModel.Option] = []
        
        for title in options.keys {
            for optionItem in options[title]! {
                if let items = optionItem.option.items {
                    output += items.filter {
                        $0.isSelected == true
                    }
                }
            }
        }
        
        return output
    }
    
}
